import axios from 'axios'
import { Toast } from 'vant'
interface ServiceFace {
  baseURL: string
  timeout: number
}
const createService: ServiceFace = {
  baseURL: 'api',
  timeout: 1000
}
const service = axios.create(createService)
service.interceptors.request.use(config => {
  const token = window.localStorage.getItem('token')
  if (token) {
    config.params = {
      token
    }
  }
  return config
}, error => {
  Promise.reject(error)
})

service.interceptors.response.use(response => {
  const res = response.data
  if (response.status !== 200) {
    return Promise.reject(new Error(res.message || 'Error'))
  } else {
    if (res.code === 200) {
      return res.result
    } else {
      Toast(res.success)
    }
  }
  return response
}, error => {
  Promise.reject(error)
})
export default service